/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = wtc_tcb_tdom.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2015/08/26 by songbb                               */
/*******************************************************************/

#include "conntrack.h"
#include "frct_comm.h"

#include "../wtc_core.h"
#include "../wtc_comm.h"
#include "../wtc_mar.h"
#include "wtc_tcb_tdom.h"

int wtc_tcb_tdom_tmpostrecv(Conntrack *p_st_conn
                            , Apm_packet *p_analyze_in
                            , L7session_wtc *p_st_sess
                            , L7_wtc_packet *p_st_wtc_packet
                            , int i_recv_size
                            , int i_hint_index)
{
    WTC_PRINT_INFO("wtc_tcb_tdom_tmpostrecv...\n");
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_wtc_private *p_st_session_private = (L7session_wtc_private *) frct_get_session_private(p_st_session);
    L7_wtc_stmt *p_st_wtc_stmt = p_st_session_private->p_st_wtc_stmt;
    char p_scratch[150] = {'\0'};
    unsigned int ui_service_len = 0;
    unsigned int ui_reqid = 0;
    unsigned int ui_convid = 0;
    unsigned int ui_seqnum = 0;
    unsigned int ui_info = 0;
    unsigned int ui_flag = 0;
    unsigned int ui_tpevent = 0;
    unsigned int ui_msgprio = 0;
    char *p_tmp = NULL;

    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &(p_st_wtc_stmt->ui_opcode), BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_DBG("opcode:[%s]\n", WTC_OPCODE2STR(p_st_wtc_stmt->ui_opcode));
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_reqid, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_reqid:[%08x]\n", ui_reqid);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_convid, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_convid:[%08x]\n", ui_convid);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_seqnum, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_seqnum:[%08x]\n", ui_seqnum);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_info, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_info:[%08x]\n", ui_info);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_flag, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_flag:[%08x]\n", ui_flag);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_tpevent, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_tpevent:[%u]\n", ui_tpevent);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &(p_st_wtc_stmt->ui_diagnostic), BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_diagnostic:[%u]\n", p_st_wtc_stmt->ui_diagnostic);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &(p_st_wtc_stmt->ui_tpurcode), BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_tpurcode:[%u]\n", p_st_wtc_stmt->ui_tpurcode);
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &(p_st_wtc_stmt->ui_errdetail), BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_errdetail:[%u]\n", p_st_wtc_stmt->ui_errdetail);
    if (bytebuff_skip(p_st_wtc_packet->p_st_packet, 4) != FR_SUCC)
        goto error;
    if (bytebuff_get_int(p_st_wtc_packet->p_st_packet, 4, &ui_msgprio, BYTE_BIG_ENDIAN) != FR_SUCC)
        goto error;
    WTC_PRINT_ALERT("ui_msgprio:[%08x]\n", ui_msgprio);
    if (bytebuff_skip(p_st_wtc_packet->p_st_packet, 16) != FR_SUCC)
        goto error;

    if (p_st_wtc_packet->i_is_req == FR_YES)
    {
        wtc_free((void **) &(p_st_sess->p_service_name));
        if (wtc_mar_decode_string(p_st_sess, p_scratch, 150, p_st_wtc_packet, &(p_st_sess->p_service_name), &ui_service_len) != FR_SUCC)
            goto error;
        if (p_st_sess->p_service_name)
            WTC_PRINT_FATAL("p_service:[%s]\n", p_st_sess->p_service_name);
    }
    else
    {
        if (wtc_mar_decode_string(p_st_sess, p_scratch, 150, p_st_wtc_packet, &p_tmp, &ui_service_len) != FR_SUCC)
            goto error;
        wtc_free((void **) &p_tmp);
    }

    return FR_SUCC;
error:
    if (p_st_wtc_packet->i_is_req == FR_YES)
        wtc_free((void **) &(p_st_sess->p_service_name));
    return FR_FAIL;
}

int wtc_tcb_tdom_tmpostrecv_fad(char *p_payload
                                , unsigned int ui_payload_len
                                , unsigned int *p_ui_offset
                                , int *p_i_direction
                                , int i_recv_size
                                , int i_hint_index)
{
    WTC_PRINT_INFO("wtc_tcb_tdom_tmpostrecv_fad...\n");
    unsigned int ui_opcode = 0;
    if (wtc_get_int(p_payload, ui_payload_len, p_ui_offset, &ui_opcode) != FR_SUCC)
        goto error;
    *p_i_direction = wtc_analyze_direction_by_opcode(ui_opcode);
    if (*p_i_direction != FR_DIRECT_UNKNOWN)
        return FR_SUCC;
    if (wtc_skip(p_payload, ui_payload_len, p_ui_offset, 60) != FR_SUCC)
        goto error;
    if (wtc_skip_string(p_payload, ui_payload_len, p_ui_offset) != FR_SUCC)
        goto error;

    return FR_SUCC;
error:
    return FR_FAIL;
}
